/*
 * Value.java
 *
 * Created on 7 novembre 2007, 12:00
 */
package com.reimscopie ;

import java.io.* ;
import java.util.* ;


/**
 * This abstract class sould be extended by all value object.
 */
public abstract class Value implements Serializable {
  
  // database id
  private long id ;
  private boolean hasId ;
  
  
  /**
   * Create a new <code>Value</code> instance with no database image.
   */
  Value() {
    
    hasId = false ;
    
  }
  
  
  /**
   * Create a new <code>Value</code> instance wich has a database image.
   *
   * @param id The image id.
   */
  Value(long id) {
    
    this.id = id ;
    hasId = true ;
    
  }
  
  
  /**
   * Returns if the value has a database ID.
   *
   * @return <code>true</code> if the value ahs an id.
   */
  public boolean hasId() {
    return hasId ;
  }
  
  /**
   * Returns a database ID for the instance.
   * 
   * @return The account ID.
   */
  public long getId() {
    return id ;
  }
  
  /**
   *Define value ID for the instance.
   *
   *@param The new ID value
   *
   */
  public void setId(long id) {
      this.id = id;
  }
  
  
  /**
   * Return a string representation of this object. All attributes of the
   * value are include.
   *
   * @return A string representation.
   *
   * @see #toMap
   */
  @Override
  public String toString() {
    
    Map<String,String> map = toMap() ;
    
    StringBuilder builder = new StringBuilder() ;
    
    // class
    builder.append(getClass().getName()) ;
    builder.append('[') ;
    
    if (map == null)
      builder.append("no attribute map") ;
    else {
      Set<String> keys = map.keySet() ;
      for (String key : keys) {
        String value = map.get(key) ;
        builder.append(key) ;
        builder.append('=') ;
        builder.append(value) ;
        builder.append(',') ;
      }
    }
    
    if (builder.charAt(builder.length()-1) == ',')
      builder.deleteCharAt(builder.length()-1) ;
    
    builder.append(']') ;
    
    return builder.toString() ;
    
  }
  
  
  /**
   * Create a new map with all attributes of this value.
   *
   * @return A string-string map with the named attributes.
   */
  public abstract Map<String,String> toMap() ;
  
}
